﻿using Shape.Mvp;
using Shape.Mvp.Commands;
using SimpleTasks.Persistence;
using SimpleTasks.Presenters;
using System.Windows.Forms;

namespace SimpleTasks.Commands.SimpleTaskList
{
   public class ApplicationStartCommand : ICommand
   {
      private IApplicationController _applicationController;
      private IUnitOfWork _unitOfWork;
      private SimpleTaskListPresenter _listPresenter;
      private SimpleTaskItemPresenter _itemPresenter;

      public ApplicationStartCommand(
         IApplicationController applicationController,
         IUnitOfWork unitOfWork,
         SimpleTaskListPresenter listPresenter, 
         SimpleTaskItemPresenter itemPresenter)
      {
         _applicationController = applicationController;
         _unitOfWork = unitOfWork;
         _listPresenter = listPresenter;
         _itemPresenter = itemPresenter;
      }

      public void Execute()
      {
         _unitOfWork.Start();

         _listPresenter.Initialise();
         _itemPresenter.Initialise();

         var shell = new MainWindow();

         shell.splitContainer1.Panel1.Controls.Add((Control)_listPresenter.View);
         shell.splitContainer1.Panel1.Controls[0].Dock = DockStyle.Fill;

         shell.splitContainer1.Panel2.Controls.Add((Control)_itemPresenter.View);
         shell.splitContainer1.Panel2.Controls[0].Dock = DockStyle.Fill;

         _applicationController.Show(shell).AsMainWindow();

         _unitOfWork.End();
      }
   }
}
